﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.ComponentModel;

namespace gChartSharp.Core
{
    public static class EnumExtensions
    {
        /// <summary>
        /// Gets the string from the Description attribute if it exists for an Enum
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string GetDescriptionAttribute(this Enum value)
        {
            FieldInfo fi = value.GetType().GetField(value.ToString());
            DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
            if (attributes.Length > 0)
            {
                return attributes[0].Description;
            }
            else
            {
                return value.ToString();
            }
        }
    }
}
